package com.ludomancy.hugealien;

import java.awt.Graphics;
import java.awt.Point;

import com.ludomancy.gamelib.Accumulator;
import com.ludomancy.gamelib.ImageStrip;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.Utils;

public class Balloon extends BasicEntity
{
	private ImageStrip balloon;
	private ImageStrip icons;
	public Uruguayan uruguayan;
	private boolean onLeftSide = false;
	private Accumulator expressing = new Accumulator(3000);
	public boolean visible = true;
	
	public Balloon(Uruguayan uruguayan)
	{
		this.uruguayan = uruguayan;
		balloon = new ImageStrip(ResourceManager.getImage("balloon.png"), 23, 22);
		icons = new ImageStrip(ResourceManager.getImage("icons.png"), 13, 11);
		if(uruguayan.area().x < Game.WORLD_WIDTH/2)
		{
			onLeftSide = true;
		}
		x = uruguayan.x;
		y = uruguayan.y;
	}
	
	public void express()
	{
		expressing.reset();
		visible = true;
	}
	
	public boolean isVisible()
	{
		return visible;
	}
	
	public void update()
	{
		if(visible)
		{
			expressing.update();
			if(expressing.isDone())
			{
				visible = false;
			}
		}
	}
	
	public void paint(Graphics g, int px, int py)
	{
		if(isVisible() || Game.game.isOver)
		{
			if(uruguayan.soul.getMood() == Soul.MOOD_OK && !Game.game.isOver)
			{
				return;
			}
			Point iconCenter = null;
			
			py = y - 2;
			if(onLeftSide)
			{
				px = x + 3;
			}
			else
			{
				px = x - 3;
			}
			
			if(onLeftSide)
			{
				balloon.paint(g, 0, px, py, Utils.ANCHOR_BOTTOM);
				iconCenter = new Point(11,-11);
			}
			else
			{
				balloon.paint(g, 1, px, py, Utils.ANCHOR_BOTTOM|Utils.ANCHOR_RIGHT);
				iconCenter = new Point(-12,-11);
			}
			
			int icon = 0;
			int mood = uruguayan.soul.getMood();
			if(Game.game.isOver)
			{
				if(uruguayan.soul.hasInitialRelationships())
				{
					mood = Soul.MOOD_HEARTBROKEN;
				}
				else
				if(uruguayan.soul.hasInitialRelationships() && !uruguayan.soul.newRelationships.isEmpty())
				{
					mood = Soul.MOOD_HAPPY;
				}
				
			}
			if(mood == Soul.MOOD_SHOCKED)
			{
				switch(uruguayan.soul.shockType)
				{
					case Soul.EVENT_NEW_LOVE:
						icon = 0;
						break;
					case Soul.EVENT_LOST_RELATIONSHIP:
					case Soul.EVENT_DEATH:
						icon = 1;
						break;
					case Soul.EVENT_SUICIDE:
						icon = 2;
						break;
				}
			}
			else
			{
				switch(mood)
				{
					case Soul.MOOD_HAPPY:
						icon = 0;
						break;
					case Soul.MOOD_HEARTBROKEN:
						icon = 3;
						break;
				}
			}
			
			icons.paint(g, icon, px + iconCenter.x, py + iconCenter.y, Utils.ANCHOR_HCENTER|Utils.ANCHOR_VCENTER);
		}
	}
}
